package com.javasm.service.stockService.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.stockCtriteria.PutStorageCriteria;
import com.javasm.domin.entity.stockEntity.PutStorageGoods;
import com.javasm.domin.entity.stockEntity.PutStorageImel;
import com.javasm.domin.entity.stockEntity.PutStorageInfo;
import com.javasm.domin.vo.stockVo.PutStorageInfoVo;
import com.javasm.mapper.stockMapper.PutStorageGoodsMapper;
import com.javasm.mapper.stockMapper.PutStorageImelMapper;
import com.javasm.mapper.stockMapper.PutStorageInfoMapper;
import com.javasm.service.stockService.PutStorageInfoService;
import com.javasm.transfer.stockTransfer.PutStorageInfoTransfer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【stock_purchase_storage_info(采购入库信息)】的数据库操作Service实现
 * @createDate 2021-11-24 21:40:27
 */
@Service
@Transactional
public class PutStorageInfoServiceImpl extends ServiceImpl<PutStorageInfoMapper, PutStorageInfo>
        implements PutStorageInfoService {
    @Resource
    private PutStorageInfoMapper putStorageInfoMapper;
    @Resource
    private PutStorageInfoTransfer putStorageInfoTransfer;
    @Resource
    private PutStorageInfoService putStorageInfoService;
    @Resource
    private PutStorageGoodsMapper putStorageGoodsMapper;
    @Resource
    private PutStorageImelMapper putStorageImelMapper;

    @Override
    public PageResult<PutStorageInfoVo> selectPage(PutStorageCriteria putStorageCriteria) {
        String purchaseCode = putStorageCriteria.getPurchaseCode();
        String buye = putStorageCriteria.getBuye();
        Date purchaseOrderTime = putStorageCriteria.getPurchaseOrderTime();
        String supplierCode = putStorageCriteria.getSupplierCode();
        String supplierName = putStorageCriteria.getSupplierName();
        Integer purchaseTypeId = putStorageCriteria.getPurchaseTypeId();
        LocalDateTime startTime = putStorageCriteria.getStartTime();
        LocalDateTime endTime = putStorageCriteria.getEndTime();
        LambdaQueryWrapper<PutStorageInfo> lambda = new QueryWrapper<PutStorageInfo>().lambda()
                .and(!StringUtils.isEmpty(purchaseCode), adminLambdaQueryWrapper -> adminLambdaQueryWrapper.like(PutStorageInfo::getPurchaseCode, purchaseCode))
                .and(!StringUtils.isEmpty(buye), adminLambdaQueryWrapper -> adminLambdaQueryWrapper.like(PutStorageInfo::getBuye, buye))
                .and(!StringUtils.isEmpty(supplierName), adminLambdaQueryWrapper -> adminLambdaQueryWrapper.like(PutStorageInfo::getSupplierName, supplierName))
                .and(!StringUtils.isEmpty(purchaseTypeId), adminLambdaQueryWrapper -> adminLambdaQueryWrapper.like(PutStorageInfo::getPurchaseTypeId, purchaseTypeId))
                .and(!StringUtils.isEmpty(supplierCode), adminLambdaQueryWrapper -> adminLambdaQueryWrapper.like(PutStorageInfo::getSupplierCode, supplierCode))
                .between(!StringUtils.isEmpty(purchaseOrderTime), PutStorageInfo::getPurchaseOrderTime, startTime, endTime);
        PageHelper.startPage(putStorageCriteria.getCurrentPage(), putStorageCriteria.getPageSize());
        List<PutStorageInfo> putStorageInfos = putStorageInfoMapper.selectList(lambda);
        PageInfo<PutStorageInfo> putStorageInfoPageInfo = new PageInfo<>(putStorageInfos);
        return new PageResult<>(putStorageInfoPageInfo.getTotal(), putStorageInfoTransfer.toVO(putStorageInfos));
    }

    @Override
    public void createBatchExcel(HttpServletResponse response, List<Integer> ids) {

        String fileName = "商品入库单";
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
        List<PutStorageInfo> putStorageInfos = putStorageInfoMapper.selectBatchIds(ids);
        ArrayList<List<PutStorageGoods>> lists1 = new ArrayList<>();
        for (Integer id : ids) {
            lists1.add(putStorageGoodsMapper.findByIdGoods(id));
        }
        ArrayList<List<PutStorageImel>> lists = new ArrayList<>();
        for (List<PutStorageGoods> putStorageGoods : lists1) {
            for (PutStorageGoods putStorageGood : putStorageGoods) {
                lists.add(putStorageImelMapper.findImelListById(putStorageGood.getPutStorageImelId()));
            }
        }

        ArrayList<PutStorageInfo> putStorageInfos1 = CollUtil.newArrayList(putStorageInfos);
        ExcelWriter writer = ExcelUtil.getWriter();
        writer.renameSheet("商品入库信息");
        writer.merge(1, "销售出库信息");
        writer.write(putStorageInfos1, true);

        writer.setSheet("商品入库详细信息");
        writer.merge(1, "商品入库详细信息");
        for (List<PutStorageGoods> putStorageGoods : lists1) {
            writer.write(putStorageGoods, true);
        }


        writer.setSheet("商品入库串号信息");
        writer.merge(1, "商品入库串号信息");
        for (List<PutStorageImel> list : lists) {
            writer.write(list, true);
        }

        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.flush(outputStream);
        writer.close();

    }

}




